<?php
/**
 * XIE--Scorpion
 *
 * 管理员管理
 */

namespace App\Http\Controllers\admin;

use App\Models\Admin\User;
use DB,Session,Url,Input,Redirect;
use App\Http\Controllers\Controller;
/**
* 
*/
class AdminController extends Controller
{
	/*
	 *管理员列表
	 */
	public function adminList()
	{
		if(Input::has('do')){
            //添加,删除     
            $d=Input::get('do');
            if($d=='in'){
                $all=Input::all();
                unset($all['_token']);
                unset($all['do']);
                $all['admin_pwd']=md5($all['admin_pwd']);
                if(User::checkTable('admin_user',$d,$all)){
                	return Redirect::action('admin\AdminController@adminList');
                }    
            }else{
                $v=Input::get('id');
                User::checkTable('admin_user',$d,$v,'admin_id');
                echo 1;            	
            }
            
		}else{
			//判断页面
			if(Input::has('d')){
				//添加
				$v='d';
				$list1=User::roleList();
			}else{
				//展示
				$list=User::adminList();
				$v='z';
			}

			return view('admin/adminlist',compact('list','v','list1'));

		}	

	}
     
    /*
     *角色管理
     */
	public function roleList()
	{
		if(Input::has('do')){

            //添加,删除
            $d=Input::get('do');
            if($d=='in'){
                $all=Input::all();
                unset($all['_token']);
                unset($all['do']);
                //var_dump($all);die;
                if(User::checkTable('admin_role',$d,$all)){

                	return Redirect::action('admin\AdminController@roleList');

                }    
            }else{
                $v=Input::get('id');
                User::checkTable('admin_role',$d,$v,'admin_rid');            	
            }

		}else{
	        //判断页面
			if(Input::has('d')){
				//添加
				$v='d';
			}else{
				//展示
				$list=User::roleList();
				$v='z';
			}

			return view('admin/rolelist',compact('list','v'));
		
		}
		
	}

	/*
	 *分配权限
	 */
	public function powerList()
	{
        if(Input::has('_token')){
            $all=Input::all();
            unset($all['_token']);
            unset($all['rid']);
            if(empty($all)){

                return Redirect::action('admin\AdminController@roleList');

            }else{
                
                $u=Input::get('rid');
                User::powerAdd($u,$all['admin_pid']);

                return Redirect::action('admin\AdminController@roleList');
                
            }
            
        }

        //角色权限
        $rid=Input::get('rid');
        $u_power=User::powerUser($rid);
        //全部权限
        $list=User::powerList();
        if(empty($u_power)){
            foreach($list as $k => $v ){
                $list[$k]->c='0';
            }
        }else{
            foreach ($list as $key => $val) {
                if(in_array($val->admin_pid,$u_power)){
                    $list[$key]->c='1';
                }else{
                    $list[$key]->c='0';
                }
            }
        }
        $list=$this->digui($list,0,0);
        
		return view('admin/powerlist',compact('list','rid'));

	}

    /*
     *递归
     */
    public function digui($data,$path,$leval)
    {
    	static $arr=array();
    	foreach($data as $v)
        {
            if($v->admin_power_level==$path)
            {
                $v->leval=$leval;
                $arr[]=$v;
                $this->digui($data,$v->admin_pid,$leval+1);
            }
        }
        return $arr;

    }

    
    
}
?>